package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.ProductAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.tools.CheckNetWork;

public class AllproductActivity extends BaseActivity {
	
	private ActivityTitle title;
	PullToRefreshListView listview; 
	
	ProductAdapter adapter;
	String cid;
	int page =1;
	 long time=0;
	 private JSONArray jsonArray;
	private TextView emptyview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_allproduct);
		
		title = (ActivityTitle) findViewById(R.id.infox_product);
		title.setTitle("全部产品");
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleSetting, title.titleRefresh, title.titleOther,title.rightLayout, title.titleMsg);
		getintentdata();
		getdata();
		listview=(PullToRefreshListView) findViewById(R.id.id_product_listview);
		adapter=new ProductAdapter(this,jsonArray );
		listview.setAdapter(adapter);
		emptyview = new TextView(this);
		emptyview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		emptyview.setText("加载中...");
		emptyview.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		emptyview.setVisibility(View.GONE);
		listview.setEmptyView(emptyview);
		
		
		
		listview.setOnRefreshListener(new OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {

				if (CheckNetWork.isConnected(AllproductActivity.this)) {
					page = 1;
					time = 0;
					getdata();
				} else {
					ToastKit.showShort(AllproductActivity.this, "请连接网络");
				}

			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {

				if (CheckNetWork.isConnected(AllproductActivity.this)) {
					page++;
					getdata();

				} else {
					ToastKit.showShort(AllproductActivity.this, "请连接网络");
				}
			}

		});
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				
				Intent intent=new Intent(AllproductActivity.this,ProductInforActivity.class);
				try {
					intent.putExtra("id",jsonArray.getJSONObject(arg2-1).getString("id"));
					startActivity(intent);
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
	}

	private void getintentdata() {
		// TODO Auto-generated method stub
		cid=getIntent().getStringExtra("cid");

	}

	private void getdata() {
		// TODO Auto-generated method stub
		ApiParam param = ApiParam.create().addParam("cid", cid).addParam("page", page);
		if (time != 0) {
			param.addParam("time", time);
		}
		Api.get("enterprise/products", param, new ApiCallback(this, true) {

			

			@Override
			public void handleSuccess(ApiResult result) {

				if (result.getCode() == 100) {
					time = result.getTime();
					if (page == 1) {
                       if(result.getDatas()==null||result.getDatas().length()<=0){
                    	   emptyview.setText("暂时没有产品");
                       }
						
						
						jsonArray = result.getDatas();
						adapter.setData(result.getDatas());
					} else {
						if (result.getDatas() == null || result.getDatas().length() <= 0) {
							page--;
						} else {
							try {
								jsonArray = AppKit.joinJSONArray(jsonArray, result.getDatas());
								adapter.appendData(result.getDatas());
							} catch (JSONException e) {
								Log.e(TAG, e.getMessage(), e);
							}
						}
					}
					listview.onRefreshComplete();
				} else{
					emptyview.setText("加载失败，请稍后重试");
					listview.onRefreshComplete();
				}
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				listview.onRefreshComplete();
				Log.e("NJ-new", "failed");
				emptyview.setText("加载失败，请稍后重试");
				listview.onRefreshComplete();

			}
		}, this);
		
	}
	
	

}
